<?php
class CategoryAction extends BaseAction{
	function index(){
		//$cate = new CategoryModel();
		//$list = $cate->relation(true)->find(1);
		//dump($list);

		$cate = new CategoryViewModel();
		 $list = $cate->select();
		 /*
		 //dump($list);
		 
		 //$cate->getError();
		 */
		$this->assign("ulist",$list);
		$this->display();
	}

	/**
	 * 添加栏目页面
	 *
	 * @param 无
	 * @return 无
	 *
	 */
	function add(){
		//获取单元列表
		$section = new SectionModel();
		$list = $section->order(id)->select();
		$this->assign("slist", $list);
			
		$this->display();
	}

	/**
	 * 编辑栏目页面
	 *
	 * @param 无
	 * @return 无
	 *
	 */
	function edit(){
		$id = $_GET['id'];
		if(!empty($id)){
			$artId = $id; 
			$cate = new CategoryModel();
			$data = $cate->getById($artId);
			$this->assign("cate", $data);
			
			//获取单元列表
			$section = new SectionModel();
			$list = $section->order(id)->select();
			$this->assign("slist", $list);
			
		}else{
			echo '请选择要编辑的栏目！';
		}
		$this->display();
	}
	/**
	 * 添加栏目信息
	 *
	 * @param 无
	 * @return 无
	 *
	 */
	function insert(){
		$cate = new CategoryModel();
		$data = $cate->create();
		dump($data);
		if($data){
			if($cate->add()){
				echo "添加栏目成功,栏目编号:".$cate->getLastInsID();
			}else {
				echo "添加栏目失败,错误信息:".$cate->getDbError();
			}
		} else{
			echo  $cate->getError();
		}
			
			
	}

	/**
	 * 更新栏目信息
	 *
	 * @param 无
	 * @return 无
	 *
	 */
	function update(){
		$cate = new CategoryModel();
		$data = $cate->create();
		dump($data);
		if($data){
			$flag = $cate->save();
			if($flag){
				echo "更新栏目成功!";
			} else{
				echo "更新栏目失败：". $cate->getDbError();
			}
		}else{
			echo "更新栏目失败：".$cate->getError();
		}
	}
	/**
	 * 删除栏目
	 *
	 * @param 无
	 * @return 无
	 *
	 */
	function delete(){
		$id = $_POST['id'];
		
		if (! empty ( $id ) && is_array ( $id )) {
			$cate = new CategoryModel();
			$artId = implode ( ',', $id );
			$flag = $cate->where ( 'id in (' . $artId . ')' )->delete();
			if ($flag) {
				//$this->assign('jumpUrl',__URL__.'/index');
				//$this->success('操作成功');
				echo "删除栏目成功!";
			} else {
				echo $cate->getDbError();
			
		//$this->error('操作失败：'.$cate->getDbError());
			}
		} else {
			echo '请选择删除的栏目！';
		}
	}
}
?>